home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c,comp.lang.c++
- Subject: Re: returning an array from function
- Date: Tue, 09 Apr 96 11:17:58 GMT
- Organization: none
- Message-ID: <829048678snz@genesis.demon.co.uk>
- References: <4jstd8$kh0@news1.sunbelt.net> <Pine.A32.3.91.960407035313.115982E-100000@magritte.its.rpi.edu>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <Pine.A32.3.91.960407035313.115982E-100000@magritte.its.rpi.edu>
- hashik@rpi.edu "Kengo Hashimoto" writes:
-
- >Just do a call-by-reference to the array, or return a pointer to the
- >first member of the array. Here are examples:
- >
- >call-by-reference:
- >
- >void foo(int &[]); // just pass the array in there, manipulate it
- > // within foo(), and the original copy would be
- > // modified as well.
-
- Note the code above is only meaningful in C++ (this was cross-posted to
- comp.lang.c). In C you can achieve a similar effect by passing a pointer.
-
- >returning a pointer:
- >int *foo();
- >
- >int bar[arraysize];
- >
- >bar = foo();
-
- You can't assign to an array.
-
- >foo() {
-
- int *foo() {
-
- > foo2[arraysize];
-
- int foo2[arraysize];
-
- > return &(foo2[0]);
- >}
-
- foo2 is a local variable which is destroyed when the function returns so
- a pointer to it is indeterminate and can't be used in the caller.
-
- >Those two approaches should work. Remember, the name of an array without
- >the [] is simply a pointer to the array.
-
- In a value context (there are a number of contexts where an array name
- can appear but not be used as a value) an array name (or other array lvalue)
- evaluates to a pointer to the first element of the array. This is not the
- same thing as a pointer to the array.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-